import { Message } from 'element-ui';
import { apis } from '../../../config/request.config.js';
import { request } from '../../common/request.js';
import { deepClone, formatMoney } from '../../common/utils.js';
const records = [
    {
        balanceId: '189349238479235',
        investCount: '89998.12',
        investPerson: '乔布斯',
        investCountBefore: '1000.12',
        investCountAfter: '90998.24',
        investTime: '20190624 23:58:34'
    },
    {
        balanceId: '189349238479235',
        investCount: '89998.12',
        investPerson: '加索尔',
        investCountBefore: '1000.12',
        investCountAfter: '90998.24',
        investTime: '20190624 23:58:34'
    }
];
export default {
    data() {
        return {
            sizeModel: 'large',
            form: {
                enterPrise: '',
                countList: [],
                investCount: formatMoney(20000000, 2),
                balance: formatMoney(500000000, 2),
                balanceId: 189349238479235,
                totalCount: ''
            },
            record: {
              size: 10,
              page: 1,
            },
            recordList: [],
            recordTotal: 20,
            isShowDialog: false,
            isShowRecords: false,
            available_balance: formatMoney(500000000, 2),
            isSuccess: false,
			selectCompany:[],
			value1:"",
			unpaidBills:[],
			unpaidBillsSelected:[],
			totalCount:0,
			company_id:-1
        }
    },

    components: {},

    computed: {
        
    },

    filters: {
        filterMoney(val) {
            return formatMoney(val);
        }
    },
    methods: {
        submit() {
            this.isSuccess = !this.isSuccess;
			
			if(!this.value1){
				Message({
					type:'error',
					message:'请先选择企业'
				})
				return false;
			}
			
			if(this.unpaidBillsSelected.length == 0){
				Message({
					type:'error',
					message:'请选择账单'
				})
				return false;
			}
			
			request({
				url:'addCredit',
			    method: "post",
			    data:{
			    	bill_ids:this.unpaidBillsSelected,
					company_id:this.value1
			    }
			}).then(res=>{
				if(res){
					this.isShowDialog = true;
					this.unpaidBills = []
					this.unpaidBillsSelected = []
					this.getPreBillList(this.company_id)
				}
			})
        },
        formatThisMoney(val){
        	return formatMoney(val)
        },
        toInvoiceList() {
            this.$router.push({
                path: '/invoiceInquiry'
            })
        },
        getInvestRecord() {
            this.searchRecord();
            this.isShowRecords = true;
        },
        searchRecord() {
            this.recordList = Array(10).fill(records[this.record.page - 1]);
            this.recordList = this.recordList.map((item, index) => {
                let listItem = deepClone(item);
                listItem.balanceId += index;
                return listItem;
            });
            
            this.recordTotal = 20;
        },
        pageSearchRecord(val) {
          this.record.page = val;
          this.searchRecord()
        },
        handleSizeChangeRecord(val) {
          this.record.size = val;
          this.searchRecord();
        },
        getSum(total, num) {
            return Number(total?total:0) + Number(num?num:0);
        },
        setTotalPrice() {
        	if(this.unpaidBills.length == 0){
        		this.totalCount = formatMoney(0)
    		}else{	
				let totalArr = this.unpaidBills.map(item=>{
					if(this.unpaidBillsSelected.indexOf(item.id) > -1){
						return item.disable_balance
					}
				})
				console.log(totalArr)
	            this.totalCount = formatMoney(totalArr.reduce(this.getSum));	
	       	}
        },
		getEnterpriseList(){
			request({
				url:'getEnterpriseList',
				method:'get',
			}).then(res=>{
				if(res.code == 0){
					this.selectCompany = res.list
				}
			})
		},
		getPreBillList(val){
			request({
				url:'getPrebillList',
				method:'get',
				params:{
					company_id:val
				}
			}).then(res=>{
				if(res.code == 0){
					this.unpaidBills = res.list
				}
			})
		},
		selectChange(val){
			this.company_id = val
			this.unpaidBills = []
			this.unpaidBillsSelected = []
			this.setTotalPrice()
			this.getPreBillList(val)
		}
		
    },
    mounted(){
		this.getEnterpriseList()
		
        if (this.$route.query.isShowRecords) {
            this.getInvestRecord();
        }
    }
}